package tpo.lmhconsumer.lmhconsumer.controller;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import dto.Dto;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import pojo.Album;
import pojo.Sort;
import pojo.Unit;
import pojo.Wares;
import tpo.lmhconsumer.lmhconsumer.service.WaresService;
import until.FtpUtil;


import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * 商品、商品分类
 */
@RestController
public class WaresController {
    @Autowired
    WaresService waresService;
    @Resource

    RedisTemplate<String,Object> redisTemplate;

    FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig(
            "192.168.2.80",
            21,
            "ftpuser",
            "123456");

    @RequestMapping("test")
    public int test(Wares wares,@RequestParam("file") MultipartFile multipartFile[]){
        return 1;
    }
    @RequestMapping("test2")
    public String test2(HttpServletResponse response){
        Cookie cookie=new Cookie("sessionId","CookieTestInfo");
        response.addCookie(cookie);
        return "获得cookies信息成功";
    }

    @RequestMapping("delWares")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto delWares(@RequestBody Map map) {
        Dto dto=waresService.delWares(map);
        return dto;
    }

    @RequestMapping("getWaresList")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getWaresList(@RequestBody Map map) {
        Dto dto=waresService.getWaresList(map);
        return dto;
    }

    @RequestMapping("getWaresList2")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getWaresList2(@RequestBody Map map) {
        Dto dto=waresService.getWaresList2(map);
        return dto;
    }

    @RequestMapping("getWaresList3")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getWaresList3(@RequestBody Map map) {
        Dto dto=waresService.getWaresList3(map);
        return dto;
    }
    @RequestMapping("getWares")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getWares(@RequestBody Map map) {
        Dto dto=waresService.getWares(map);
        return dto;
    }

    @RequestMapping("getWares2")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getWares2(@RequestBody Map map) {
        Dto dto=waresService.getWares2(map);
        return dto;
    }

    @RequestMapping("getWares_page")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getWares_page(@RequestBody Map map) {
        return waresService.getWares_Page(map);
    }

    @RequestMapping("updataWares")
    @HystrixCommand(fallbackMethod = "rd_wares")
    public Dto updateWares(Wares wares) {
        return waresService.updateWares(wares);
    }

    @RequestMapping("addWares")
    public Dto addWares(Wares wares,@RequestParam("file") MultipartFile[] multipartFile) {
        List<String> list=new ArrayList<>();
        if (multipartFile.length!=0){
            for (MultipartFile file : multipartFile){
                String oldername=file.getOriginalFilename();
                String suffix= FilenameUtils.getExtension(oldername);
                String newname= UUID.randomUUID().toString().replace("-", "").substring(14).toLowerCase()+ "."+suffix;
                try {
                    FtpUtil.upload(ftpConfig, "/tpo/", file.getInputStream(), newname);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                list.add(newname);
            }
        }
        return waresService.addWares(wares,list);
    }

    @RequestMapping("getSort")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getSort(@RequestBody Map map) {
        return waresService.getSort(map);
    }


    @RequestMapping("updateSort")
    public Dto updateSort(@RequestParam(value = "file", required = false) MultipartFile file,
                          @RequestParam(value = "json", required = false) String json) {
        Sort sort= JSON.parseObject(json,Sort.class);
        if (file!=null){
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(14).toLowerCase()+ "."+suffix;
            try {
                FtpUtil.deleteFile(ftpConfig,"/tpo/",sort.getLog());
                FtpUtil.upload(ftpConfig, "/tpo/", file.getInputStream(), newname);
            } catch (IOException e) {
                e.printStackTrace();
            }
            sort.setLog(newname);
        }
        Dto dto=waresService.updateSort(sort);
        return dto;
    }

    @RequestMapping("delSort")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto delSort(@RequestBody Map map) {
        FtpUtil ftpUtil=new FtpUtil();
        FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
        ftpConfig.setIp("192.168.2.80");
        ftpConfig.setPort(21);
        ftpConfig.setUserName("ftpuser");
        ftpConfig.setPassword("123456");
        ftpUtil.deleteFile(ftpConfig,"/tpo/",(String)map.get("log"));
        return waresService.delSort(map);
    }

    @RequestMapping(value = "addSort")
    public Dto addSort(@RequestParam(value = "file", required = false) MultipartFile file,
                       @RequestParam(value = "json", required = false) String json){
        Sort sort= JSON.parseObject(json,Sort.class);
        if (file!=null){
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
            System.out.println(newname);
            try {
                FtpUtil.upload(ftpConfig,
                        "/tpo/",
                        file.getInputStream(),
                        newname);
            } catch (IOException e) {
                e.printStackTrace();
            }
            sort.setLog(newname);
        }
        Dto dto=waresService.addSort(sort);
        return dto;
    }

    @RequestMapping("getSort3L")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getSort3L(@RequestBody Map map) {
        List list=(List) waresService.getSort(map).getObj();
        list=this.getallSort(list);
        Dto dto=new Dto();
        dto.setObj(list);
        return dto;
    }

    @RequestMapping("getAlbum")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getAlbum(@RequestBody Map map) {
        Dto dto=waresService.getAlbum(map);
        return dto;
    }

    @RequestMapping("delAlbum")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto delAlbum(@RequestBody Map map) {
        String name=(String)map.get("name");
        FtpUtil.deleteFile(ftpConfig, "/tpo/",name);
        Dto dto=waresService.delAlbum(map);
        return dto;
    }

    @RequestMapping("addAlbum")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto addAlbum(Long id,@RequestParam("file") MultipartFile[] multipartFile) {
        List<Album> list=new ArrayList<>();
        if (multipartFile.length!=0){
            for (MultipartFile file : multipartFile){
                String oldername=file.getOriginalFilename();
                String suffix= FilenameUtils.getExtension(oldername);
                String newname= UUID.randomUUID().toString().replace("-", "").substring(14).toLowerCase()+ "."+suffix;
                try {
                    FtpUtil.upload(ftpConfig, "/tpo/", file.getInputStream(), newname);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Album album=new Album();
                album.setWaresId(id);
                album.setName(newname);
                list.add(album);
            }
        }
        Dto dto=waresService.addAlbum(list);
        return dto;
    }



    private List<Sort> getallSort(List<Sort> list){
        if (list.size()>0){
            for (Sort so : list){
                Map map=new HashMap();
                map.put("sid",so.getId());
                List childList=(List) waresService.getSort(map).getObj();
                childList=getallSort(childList);
                so.setChildList(childList);
            }
        }
        return list;
    }

    private Dto rd_map(Map map){
        Dto dto=new Dto();
        dto.setMsg("熔断");
        return dto;
    }
    private Dto rd_wares(Wares wares){
        Dto dto=new Dto();
        dto.setMsg("熔断");
        return dto;
    }
    private Dto rd_sort(Sort sort){
        Dto dto=new Dto();
        dto.setMsg("熔断");
        return dto;
    }

}
